package app.qrcode.browser.hearttoheart.adapters

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import app.qrcode.browser.hearttoheart.R

/**
 * Created by DomenCai on 2022/3/24.
 */
class SpeechAdapter : RecyclerView.Adapter<SpeechAdapter.SpeechViewHolder>() {

    private val speechList: MutableList<Speech> = mutableListOf()

    @SuppressLint("NotifyDataSetChanged")
    fun updateList(newList: ArrayList<Speech>) {
        speechList.clear()
        speechList.addAll(newList)
        notifyDataSetChanged()
    }

    inner class SpeechViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val speechTeacherName: TextView = view.findViewById(R.id.speechTeacherName)
        val speechTeacherHead: ImageView = view.findViewById(R.id.speechTeacherHead)
        val speechTeacherSays: TextView = view.findViewById(R.id.speechTeacherSays)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SpeechViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_speech, parent, false)
        return SpeechViewHolder(view)
    }

    override fun onBindViewHolder(holder: SpeechViewHolder, position: Int) {
        val speech = speechList[position]
        holder.speechTeacherName.text = speech.teacherName
        holder.speechTeacherHead.setImageResource(speech.teacherHead)
        holder.speechTeacherSays.text = speech.teacherSay
    }

    override fun getItemCount(): Int = speechList.size
}